package com.xxz.course;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RateListActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private static final String TAG = "click";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);

        //初始化数据项
        ArrayList<HashMap<String,String>> listItems = new ArrayList<HashMap<String,String>>();
        for(int i = 0; i<30; i++){
            HashMap<String,String> map = new HashMap<String,String>();
            map.put("ItemTitle","Rate= "+i);//标题文字
            map.put("ItemDetail","Detail= "+i);//详情描述
            listItems.add(map);
        }
        //生成适配器的Item和动态数组对应的元素
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItems,R.layout.list_item,new String[]{"ItemTitle","ItemDetail"},new int[]{R.id.title,R.id.detail});
        //绑定控件
        ListView listView = findViewById(R.id.mylist2);
        listView.setAdapter(listItemAdapter);
        listView.setOnItemClickListener(this);//为listView添加点击响应

        Handler handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 7) {
                    ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    //构造adapter方式
//                    SimpleAdapter listItemAdapter = new SimpleAdapter(RateListActivity2.this,list,
//                            R.layout.list_item,new String[]{"cname","cval"},new int[]{R.id.title,R.id.detail});
                    MyAdapter adapter = new MyAdapter(RateListActivity2.this,R.layout.list_item,list);
                    listView.setAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };
        MapTask task = new MapTask();
        task.setHandler(handler);

        Thread t= new Thread(task);
        t.start();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG,"onItemclick");
        //获得数据项
        ListView listView = findViewById(R.id.mylist2);
        HashMap<String,String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
        Log.i(TAG,"onItemClick: cname "+map.get("cname"));
        Log.i(TAG,"onItemClick: cval "+map.get("cval"));
        Log.i(TAG, "onItemClick: 打开新窗口");
        Intent Itemclick = new Intent(this, RateCalculateActivity.class);
        String currency = map.get("cname");
        float rate = Float.parseFloat(map.get("cval"));
        Itemclick.putExtra("cur_key", currency);
        Itemclick.putExtra("rate_key", rate);

          Log.i(TAG, "openOne: cur_key=" + currency);
          Log.i(TAG, "openOne: rate_key=" + rate);

          startActivity(Itemclick);


    }
}